****to create the variable population Gini in independent_vars.dta, first download the package adgini before running these commands
use "populations.dta"
bysort congress: adgini population
clear


****to create the variable closevote for inclusion in independent_vars.dta
use "votes.dta"
gen yesvote=.
replace yesvote=1 if cast_code==1
gen novote=.
replace novote=1 if cast_code==6
collapse (sum) yesvote novote, by(congress rollnumber)
gen closevote=0
replace closevote = 1 if abs((yesvote-novote)/(yesvote+novote)) < .2
collapse (mean) closevote, by(congress)


****to create the variable majfrac in independent_vars.dta
use "votes.dta"
keep if cast_code==1 | cast_code==2 | cast_code==5 | cast_code==6
gen voted=1
*next command collapses data to congress-member and includes a variable "voted" that is the total number of  votes cast that congress (yes, no, or paired) by a member. state_icpsr and majority are also carried over for further use.
collapse (sum) voted, by(congress state_icpsr icpsr majority)
*next command orders the data by congress and state with the member casting the most votes withiin the delegation first and so on until the least.
gsort congress state_icpsr -voted
*next command creates a variable that reflects that order (1 is most votes, 2 second most, and so on. 0 occurs for nine instances where only one senator represented a state within a congress)
quietly by congress state_icpsr: gen dup = cond(_N==1, 0, _n)
*next command drops Senators who were not in the top two in terms of votes cast for that state and congress.
drop if dup>2
merge m:1 congress state_icpsr using "populations.dta"
drop _merge
*next three commands drop senators from three secession states that cast a small number of roll calls during the 37th Congress (1861-63) before leaving the chamber. The fourth drops the Delaware delegation during the 57th Congress, where only one senator participated and cast only a single vote. See relevant note in the Codebook.
drop if congress==37 & state_icpsr==42
drop if congress==37 & state_icpsr==47
drop if congress==37 & state_icpsr==49
drop if congress==57 & state_icpsr==11
*next command drops one member for a state-congress pair when both senators are members of the majority or both are members of the minority. The remaining congress-state pairs with more than one member in the data reflect split delegations.
duplicates drop congress state_icpsr majority, force
*next command creates a new variable (redund) that takes the value of one for state-congress pairs where one senator was a member of the majority and one a member of the minority, that is, for the split delegations with two observations per congress-state remaining in the data. All other pairing have a single observation, reflecting unified delegations for either the majority or minority.
duplicates tag congress state_icpsr, generate(redund)
*next command reduces population by one-half when a delegation is split, or if only a single member from that state voted during the relevant congress.  The command is necessary to ensure that individual senators be associated with only half of the relevant state population.
replace population= population*.5 if (redund==1 | dup==0)
*next command produces aggregate population covered by the majority and minority per congress
collapse (sum) population, by(congress majority)
*next command moves the totals for the majority and minority into a single observation per congress (population0 for the minority and population1 for the majority)
reshape wide population, i(congress) j(majority)
*next command produces the population total for a congress based on the variables and Stata commands utilized in this analysis.
gen totalpop= population0 + population1
*final command produces the variable for the fraction of the population represented by members of the partisan majority in a given congress.
gen majfrac= population1/totalpop


****to create the variable cmnoties in FigA2dta
use "votes.dta"
merge m:1 congress state_icpsr using "populations.dta"
drop _merge
gen yesvote = cast_code
recode yesvote (1=1) (6=0) (else=.)
gen novote=cast_code
recode novote (6=1) (1=0) (else=.)
gen yespop= (yesvote * population)/2
gen nopop= (novote * population)/2
collapse (sum) yesvote novote yespop nopop, by(congress rollnumber)
drop if yesvote==novote
gen cmynoties=0
replace cmynoties=1 if (yesvote > novote) & (yespop< nopop)
gen cmnnoties=0
replace cmnnoties=1 if (novote >=yesvote) & (yespop>nopop)
gen cmnoties = cmynoties+cmnnoties
collapse (mean) cmnoties, by(congress)


****to create the variable cmvp in FigA2.dta
use "votes.dta"
merge m:1 congress state_icpsr using "populations.dta"
drop _merge
gen yesvote = cast_code
recode yesvote (1=1) (6=0) (else=.)
gen novote=cast_code
recode novote (6=1) (1=0) (else=.)
gen yespop= (yesvote * population)/2
gen nopop= (novote * population)/2
collapse (sum) yesvote novote yespop nopop, by(congress rollnumber)
merge m:1 congress rollnumber using "motions.dta"
drop _merge
replace yesvote = yesvote+1 if vpvote==1
replace novote = novote+1 if vpvote==6
gen cmyvp=0
replace cmyvp=1 if (yesvote > novote) & (yespop< nopop)
gen cmnvp=0
replace cmnvp=1 if (novote >=yesvote) & (yespop>nopop)
gen cmvp = cmyvp+cmnvp
collapse (mean) cmvp, by(congress)

